﻿using System.Web;

namespace PageAdmin.Utils
{
    /// <summary>
    /// session帮助类
    /// </summary>
    public class SessionHelper
    {

        /// <summary>
        /// 根据名称获取session对象
        /// </summary>
        /// <param name="sessionName"></param>
        /// <returns></returns>
        public static T Get<T>(string sessionName)
        {
            object obj=HttpContext.Current.Session[sessionName];
            if(obj!=null)
            {
                if(obj is T)
                {
                    return (T)obj;
                }
            }
            return default(T);
        }

        /// <summary>
        /// 设置session
        /// </summary>
        /// <param name="sessionName"></param>
        /// <param name="val"></param>
        public static void Add(string sessionName, object val)
        {
            Remove(sessionName);
            HttpContext.Current.Session.Add(sessionName,val);
        }

        /// <summary>
        /// 删除session
        /// </summary>
        /// <param name="sessionName"></param>
        public static void Remove(string sessionName)
        {
            HttpContext.Current.Session.Remove(sessionName);
        }

    }
}
